package com.example.tliaswebmanagement.controller;

import com.example.tliaswebmanagement.pojo.PageResult;
import com.example.tliaswebmanagement.pojo.Result;
import com.example.tliaswebmanagement.pojo.StuQueryParam;
import com.example.tliaswebmanagement.pojo.Student;
import com.example.tliaswebmanagement.service.StuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/students")
@RestController
public class StuController {
    private final StuService stuService;

    public StuController(StuService stuService) {
        this.stuService = stuService;
    }

    /**
     * 学员条件列表查询
     */
    @GetMapping
    public Result paginationStu(StuQueryParam stuQueryParam) {
        log.info("前端传递的查询参数：{}", stuQueryParam);
        PageResult<Student> PageResult = stuService.paginationStu(stuQueryParam);
        return Result.success(PageResult);
    }

    /**
     * 根据 ID 查询（数据回显）
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        log.info("前端要查询的学员ID：{}", id);
        Student stu = stuService.findById(id);
        return Result.success(stu);
    }

    /**
     * 修改学员
     */
    @PutMapping
    public Result updateById(@RequestBody Student student) {
        log.info("前端传递的修改参数：{}", student);
        stuService.updateById(student);
        return Result.success();
    }

    /**
     * 添加学员
     */
    @PostMapping
    public Result addStu(@RequestBody Student student) {
        log.info("前端需要添加的学员信息：{}", student);
        stuService.addStu(student);
        return Result.success();
    }

    /**
     * 删除学员
     */
    @DeleteMapping("/{ids}")
    public Result deleteStu(@PathVariable List<Integer> ids) {
        log.info("前端要删除的学员id：{}", ids);
        stuService.deleteStu(ids);
        return Result.success();
    }

    /**
     * 违纪处理
     */
    @PutMapping("violation/{id}/{score}")
    public Result violate(@PathVariable Integer id, @PathVariable Integer score) {
        log.info("前端传递过来的违纪学员ID与扣除分数：{}, {}", id, score);
        stuService.violateUpdate(id, score);
        return Result.success();
    }
}
